package com.example.administrator.wallet;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.view.fragment.QuotationFragment;
import com.example.administrator.wallet.view.fragment.TransactionFragment;
import com.example.administrator.wallet.view.fragment.asset.AssetFragment;
import com.example.administrator.wallet.view.fragment.mine.MineFragment;

import butterknife.Bind;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {


    public static ClassLoader instance;
    @Bind(R.id.main_container)
    FrameLayout mainContainer;
    @Bind(R.id.main_image_asset)
    ImageButton mainImageAsset;
    @Bind(R.id.main_text_asset)
    TextView mainTextAsset;
    @Bind(R.id.main_asset)
    LinearLayout mainAsset;
    @Bind(R.id.main_image_quotation)
    ImageButton mainImageQuotation;
    @Bind(R.id.main_text_quotation)
    TextView mainTextQuotation;
    @Bind(R.id.main_quotation)
    LinearLayout mainQuotation;
    @Bind(R.id.main_image_transacation)
    ImageButton mainImageTransacation;
    @Bind(R.id.main_text_transacation)
    TextView mainTextTransacation;
    @Bind(R.id.main_transacation)
    LinearLayout mainTransacation;
    @Bind(R.id.main_image_mine)
    ImageButton mainImageMine;
    @Bind(R.id.main_text_mine)
    TextView mainTextMine;
    @Bind(R.id.main_mine)
    LinearLayout mainMine;
    @Bind(R.id.bottom_container)
    LinearLayout bottomContainer;
    @Bind(R.id.activity_main)
    LinearLayout activityMain;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private AssetFragment assetFragment;
    private QuotationFragment quotationFragment;
    private TransactionFragment transactionFragment;
    private MineFragment mineFragment;
    private long exitTime = 0;
    private static Resources resources;
    private static DisplayMetrics dm;
//    private Dialog dialog;
//    private Handler mHandler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            if (msg.what == 0) {
//                dialog.dismiss();
//            }
//        }
//    };

    @Override
    public void init() {
        loadData();
        resources = getResources();
        dm = resources.getDisplayMetrics();
        selected(0);
    }

    private void loadData() {
//        Intent intent = getIntent();
//        int hide = intent.getIntExtra("hide", 0);
//        String title = getResources().getString(R.string.load);
//        String msg = getResources().getString(R.string.loadData);
//
//        String loadData = getResources().getString(R.string.loadData);
//        dialog = CustomProgressDialog.createLoadingDialog(this, loadData);//文字即为显示的内容
//        dialog.setCancelable(false);//允许返回
//        if (hide == 1) {
//            dialog.dismiss();
//        } else {
//            dialog.show();//显示
//        }
//        Thread thread = new Thread(new Runnable() {
//            public void run() {
//                try {
//                    Thread.sleep(2000);
//                    Message message = new Message();
//
//                    if (assetFragment != null && assetFragment.adapter != null) {
//                        if (assetFragment.adapter.getCount() > 0) {
//                            message.what = 0;
//                        }
//                    }
//                    mHandler.sendMessage(message);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//            }
//        });
//        thread.start();
    }

    private void selected(int i) {
        resetImage();
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                getWindow().setStatusBarColor(getResources().getColor(R.color.statu));
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                if (assetFragment == null) {
                    assetFragment = new AssetFragment();
                    transaction.add(R.id.main_container, assetFragment);
                } else {
                    transaction.show(assetFragment);
                }
                transaction.commit();
                mainImageAsset.setImageResource(R.drawable.tab_zichan_selected);
                mainTextAsset.setTextColor(getResources().getColor(R.color.tab_sel));
                break;
            case 1:
                setStatuColor();
                if (quotationFragment == null) {
                    quotationFragment = new QuotationFragment();
                    transaction.add(R.id.main_container, quotationFragment);
                } else {
                    transaction.show(quotationFragment);
                }
                transaction.commit();
                mainImageQuotation.setImageResource(R.drawable.tab_ico_faxian_sel);
                mainTextQuotation.setTextColor(getResources().getColor(R.color.tab_sel));
                break;
            case 2:
                setStatuColor();
                if (transactionFragment == null) {
                    transactionFragment = new TransactionFragment();
                    transaction.add(R.id.main_container, transactionFragment).show(transactionFragment);
                } else {
                    transaction.show(transactionFragment);
                }
                transaction.commit();
                mainImageTransacation.setImageResource(R.drawable.tab_jiaoyi_selected);
                mainTextTransacation.setTextColor(getResources().getColor(R.color.tab_sel));
                break;
            case 3:
                getWindow().setStatusBarColor(getResources().getColor(R.color.statu));
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    transaction.add(R.id.main_container, mineFragment);
                } else {
                    transaction.show(mineFragment);
                }
                transaction.commit();
                mainImageMine.setImageResource(R.drawable.tab_my_selected);
                mainTextMine.setTextColor(getResources().getColor(R.color.tab_sel));
                break;
        }
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (assetFragment != null) {
            transaction.hide(assetFragment);
        }
        if (quotationFragment != null) {
            transaction.hide(quotationFragment);
        }

        if (transactionFragment != null) {
            transaction.hide(transactionFragment);
        }

        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }
    }

    private void resetImage() {
        mainImageAsset.setImageResource(R.drawable.tab_zichan_normal);
        mainImageQuotation.setImageResource(R.drawable.tab_ico_faxian_nor);
        mainImageTransacation.setImageResource(R.drawable.tab_jiaoyi_normal);
        mainImageMine.setImageResource(R.drawable.tab_my_normal);
        mainTextAsset.setTextColor(getResources().getColor(R.color.tab_nor));
        mainTextQuotation.setTextColor(getResources().getColor(R.color.tab_nor));
        mainTextTransacation.setTextColor(getResources().getColor(R.color.tab_nor));
        mainTextMine.setTextColor(getResources().getColor(R.color.tab_nor));
    }


    @Override
    public int getLayoutResource() {
        return R.layout.activity_main;
    }


    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), R.string.exit, Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                Intent intent = new Intent();
                // 为Intent设置Action、Category属性
                intent.setAction(Intent.ACTION_MAIN);// "android.intent.action.MAIN"
                intent.addCategory(Intent.CATEGORY_HOME); //"android.intent.category.HOME"
                startActivity(intent);
            }
        }
        return true;
    }

    @OnClick({R.id.main_asset, R.id.main_quotation, R.id.main_transacation, R.id.main_mine})
    public void onViewClicked(View view) {
//        if (!dialog.isShowing()) {
            switch (view.getId()) {
                case R.id.main_asset:
                    selected(0);
                    break;
                case R.id.main_quotation:
                    selected(1);
                    break;
                case R.id.main_transacation:
                    selected(2);
                    break;
                case R.id.main_mine:
                    selected(3);
                    break;
            }
//        }
    }
}
